package com.gla.market.SearchHistoryDatabase;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gla.market.activity.AfterSearchActivity;
import com.gla.market.activity.Commodity_FYHActivity;
import com.gla.market.activity.Shop_FYHActivity;
import com.gla.market.adapter.AfterSearchAdapter;
import com.gla.market.adapter.FYH_CommodityAdapter;
import com.gla.market.constant.Constants;
import com.gla.market.entity.Commodity;
import com.gla.market.entity.MY_Type;
import com.gla.market.entity.ShopItem;
import com.gla.market.utils.NetUtil;
import com.nostra13.universalimageloader.utils.L;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class TuiJian {

    private PetShopDatabaseHelper petShopDatabaseHelper;
    List<Commodity> searchList;
    List<Commodity> petList;
    List<Commodity> petGoodsList;

    Boolean istrue1,istrue2,istrue3;

    /***
     * 推荐给搜索前页面的搜索发现
     * **/
    public List<Commodity> SearchFine(Context context,Integer user_id){

        istrue1 = true; //死循环，防止异步
        searchList = new ArrayList<>();
        petShopDatabaseHelper = new PetShopDatabaseHelper(context);
        String userId = user_id.toString();
        List<MY_Type> my_types = petShopDatabaseHelper.getUserType(userId);

        my_types = init(my_types,userId); //初始化数据，前端数据库没有的类别自动+1

        //推荐逻辑代码


        Integer all=0;
        double cat=0,dog=0,chuxing=0,weisheng=0,wanju=0,shiping=0;
        Integer cat1=0,dog1=0,chuxing1=0,weisheng1=0,wanju1=0,shiping1=0;
        for(MY_Type my_type : my_types){
            if(my_type.getType().equals("猫")) cat = my_type.getNum();
            if(my_type.getType().equals("狗")) dog = my_type.getNum();
            if(my_type.getType().equals("出行用品")) chuxing = my_type.getNum();
            if(my_type.getType().equals("卫生用品")) weisheng = my_type.getNum();
            if(my_type.getType().equals("玩具")) wanju = my_type.getNum();
            if(my_type.getType().equals("食品")) shiping = my_type.getNum();
            all = all + my_type.getNum();
        }
        Integer yu=6;

        cat1 = (int)((cat/all) * 6); if(yu>0 && cat1<1) cat1=1; if(yu>=cat1) yu=yu-cat1;
        dog1 = (int)((dog/all) * 6); if(yu>0 && dog1<1) dog1=1; if(yu>=dog1) yu=yu-dog1;
        chuxing1 = (int)((chuxing/all) * 6); if(yu>0 && chuxing1<1) chuxing1=1; if(yu>=chuxing1) yu=yu-chuxing1;
        weisheng1 = (int)((weisheng/all) * 6); if(yu>0 && weisheng1<1) weisheng1=1; if( yu>=weisheng1) yu=yu-weisheng1;
        wanju1 = (int)((wanju/all) * 6); if(yu>0 && wanju1<1) wanju1=1; if(yu>=wanju1) yu=yu-wanju1;
        shiping1 = (int)((shiping/all) * 6); if(shiping1<1 && yu>0) shiping1=1; if(yu>=shiping1) yu=yu-shiping1;

        String url = Constants.BASE_URL + Constants.GET_SEARCH_URL +Constants.GET_TUIJIAN_SEARCH+"?cat="+cat1+"&dog="+dog1+"&chuxing="+chuxing1+"&weisheng="+weisheng1+"&wanju="+wanju1+"&shiping="+shiping1;
        NetUtil.doGet(url, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("调试", "错误信息：" + e.toString());
                        Message message = Message.obtain();
                        message.what = -1;
//                            uiHandler.sendMessage(message);
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string().trim();
                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
                        Message message = Message.obtain();

                        if (!object.getString("message").contains("JWT") && object.getInteger("code")==100) {
                            JSONArray array = object.getJSONArray("data");
                            //获得商品list
                            searchList = JSONArray.parseArray(array.toString(), Commodity.class);
                            istrue1=false;

//                                message.what = 4;
                        } else {
//                                message.what = 0;
                        }
//                            uiHandler.sendMessage(message);
                    }
                }
        );
        while (istrue1);
        return searchList;
    }

    /***
     * 推荐宠物
     * **/
    public List<Commodity> PetRecommend(Context context,Integer user_id){

        istrue2 = true;
        petList = new ArrayList<>();
        petShopDatabaseHelper = new PetShopDatabaseHelper(context);
        String userId = user_id.toString();
        List<MY_Type> my_types = petShopDatabaseHelper.getUserType(userId);
        my_types = init(my_types,userId); //初始化数据，前端数据库没有的类别自动+1

        Integer all=0;
        double cat=0,dog=0;
        Integer cat1=0,dog1=0;

        for(MY_Type my_type : my_types){
            if(my_type.getType().equals("猫")){
                cat = my_type.getNum();
                all = all + my_type.getNum();
            }
            if(my_type.getType().equals("狗")){
                dog = my_type.getNum();
                all = all + my_type.getNum();
            }
        }
        Integer yu=6;

        cat1 = (int)((cat/all) * 6); if(yu>0 && cat1<1) cat1=1; if(yu>=cat1) yu=yu-cat1;
        dog1 = (int)((dog/all) * 6); if(yu>0 && dog1<1) dog1=1; if(yu>=dog1) yu=yu-dog1;

        String url = Constants.BASE_URL + Constants.GET_SEARCH_URL +Constants.GET_TUIJIAN_SEARCH+"?cat="+cat1+"&dog="+dog1+"&chuxing="+0+"&weisheng="+0+"&wanju="+0+"&shiping="+0;
        NetUtil.doGet(url, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("调试", "错误信息：" + e.toString());
                        Message message = Message.obtain();
                        message.what = -1;
//                            uiHandler.sendMessage(message);
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string().trim();
                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
                        Message message = Message.obtain();

                        if (!object.getString("message").contains("JWT") && object.getInteger("code")==100) {
                            JSONArray array = object.getJSONArray("data");
                            //获得商品list
                            petList = JSONArray.parseArray(array.toString(), Commodity.class);
                            istrue2=false;

//                                message.what = 4;
                        } else {
//                                message.what = 0;
                        }
//                            uiHandler.sendMessage(message);
                    }
                }
        );
        while (istrue2);
        return petList;
    }

    /***
     * 推荐宠物用品
     * **/
    public List<Commodity> PetGoodsRecommend(Context context,Integer user_id){

        istrue3 = true;
        petGoodsList = new ArrayList<>();
        petShopDatabaseHelper = new PetShopDatabaseHelper(context);
        String userId = user_id.toString();
        List<MY_Type> my_types = petShopDatabaseHelper.getUserType(userId);
        my_types = init(my_types,userId);

        Integer all=0;
        double chuxing=0,weisheng=0,wanju=0,shiping=0;
        Integer chuxing1=0,weisheng1=0,wanju1=0,shiping1=0;
        for(MY_Type my_type : my_types){
            if(my_type.getType().equals("出行用品")){
                chuxing = my_type.getNum();
                all = all + my_type.getNum();
            }
            if(my_type.getType().equals("卫生用品")){
                weisheng = my_type.getNum();
                all = all + my_type.getNum();
            }
            if(my_type.getType().equals("玩具")){
                wanju = my_type.getNum();
                all = all + my_type.getNum();
            }
            if(my_type.getType().equals("食品")){
                shiping = my_type.getNum();
                all = all + my_type.getNum();
            }
        }
        Integer yu=6;

        chuxing1 = (int)((chuxing/all) * 6); if(yu>0 && chuxing1<1) chuxing1=1; if(yu>=chuxing1) yu=yu-chuxing1;
        weisheng1 = (int)((weisheng/all) * 6); if(yu>0 && weisheng1<1) weisheng1=1; if( yu>=weisheng1) yu=yu-weisheng1;
        wanju1 = (int)((wanju/all) * 6); if(yu>0 && wanju1<1) wanju1=1; if(yu>=wanju1) yu=yu-wanju1;
        shiping1 = (int)((shiping/all) * 6); if(shiping1<1 && yu>0) shiping1=1; if(yu>=shiping1) yu=yu-shiping1;

        String url = Constants.BASE_URL + Constants.GET_SEARCH_URL +Constants.GET_TUIJIAN_SEARCH+"?cat="+0+"&dog="+0+"&chuxing="+chuxing1+"&weisheng="+weisheng1+"&wanju="+wanju1+"&shiping="+shiping1;
        NetUtil.doGet(url, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("调试", "错误信息：" + e.toString());
                        Message message = Message.obtain();
                        message.what = -1;
//                            uiHandler.sendMessage(message);
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string().trim();
                        com.alibaba.fastjson.JSONObject object = JSON.parseObject(result);
                        Message message = Message.obtain();

                        if (!object.getString("message").contains("JWT") && object.getInteger("code")==100) {
                            JSONArray array = object.getJSONArray("data");
                            //获得商品list
                            petGoodsList = JSONArray.parseArray(array.toString(), Commodity.class);
                            istrue3=false;

//                                message.what = 4;
                        } else {
//                                message.what = 0;
                        }
//                            uiHandler.sendMessage(message);
                    }
                }
        );
        while (istrue3);
        return petGoodsList;

    }


    /***
     * 数据初始化，类别没有的加自动设置该类别为1
     * **/
    private List<MY_Type> init(List<MY_Type> lists,String userId){

        Integer cat=0,dog=0,chuxing=0,weisheng=0,wanju=0,shiping=0;


        for(MY_Type my_type : lists){
            if(my_type.getType().equals("猫")) cat=1;
        }
        if(cat==0){
            MY_Type tt = new MY_Type();
            tt.setUserId(userId);
            tt.setType("猫");
            tt.setNum(1);
            lists.add(tt);
        }

        for(MY_Type my_type : lists){
            if(my_type.getType().equals("狗")) dog=1;
        }
        if(dog==0){
            MY_Type tt = new MY_Type();
            tt.setUserId(userId);
            tt.setType("狗");
            tt.setNum(1);
            lists.add(tt);
        }

        for(MY_Type my_type : lists){
            if(my_type.getType().equals("出行用品")) chuxing=1;
        }
        if(chuxing==0){
            MY_Type tt = new MY_Type();
            tt.setUserId(userId);
            tt.setType("出行用品");
            tt.setNum(1);
            lists.add(tt);
        }

        for(MY_Type my_type : lists){
            if(my_type.getType().equals("卫生用品")) weisheng=1;
        }
        if(weisheng==0){
            MY_Type tt = new MY_Type();
            tt.setUserId(userId);
            tt.setType("卫生用品");
            tt.setNum(1);
            lists.add(tt);
        }
        for(MY_Type my_type : lists){
            if(my_type.getType().equals("玩具")) wanju=1;
        }
        if(wanju==0){
            MY_Type tt = new MY_Type();
            tt.setUserId(userId);
            tt.setType("玩具");
            tt.setNum(1);
            lists.add(tt);
        }
        for(MY_Type my_type : lists){
            if(my_type.getType().equals("食品")) shiping=1;
        }
        if(shiping==0){
            MY_Type tt = new MY_Type();
            tt.setUserId(userId);
            tt.setType("食品");
            tt.setNum(1);
            lists.add(tt);
        }
        return lists;
    }
}
